<?php

namespace app\api\controller;

use app\common\controller\Api;
use addons\wanlshop\library\WanlSdk\Common;
use app\admin\model\User;
use think\Request;
use app\common\library\Sms;

/**
 * 定时任务
 */
class Timedtask extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    // 用户模型
    protected $userModel = null;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->userModel = new User();
    }

    /**
     * 短信通知未付款的用户
     */
    public function sendNoteNopayUsers()
    {
        return;
        // 获取配置间隔时间
        $config = get_addon_config('wanlshop');
        $nopaynote = $config['order']['nopaynote'];
        $day_timestamp = 3600 * 24 * $nopaynote;
        // 获取redis实例
        $redis = Common::redis();
        // 获取未支付订单
        $nopayOrders = $redis->lRange("order_no_pay", 0, -1);
        if(!isset($nopayOrders)) return;
        foreach ($nopayOrders as $key=>$val){
            $nopayOrdersArr = json_decode($val, true);
            // 创建订单距离当前过去了多上时间戳
            $pass_away = time() - $nopayOrdersArr['createtime'];
            // 如果pass_away大于或者等于间隔时间 就发短信提醒用户
            if($pass_away >= $day_timestamp){
                $mobile = $this->userModel->where(['id'=>$nopayOrdersArr['user_id']])->value('mobile');
                //TODO 发送短信
                if(!Sms::notice($mobile, '', '阿里云通信中的模板ID')){
                    $this->error("发送失败", $mobile, 0);
                }
            }
        }
        $this->success("短信发送成功", '', 1);
    }
}
